package com.jsh.erp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jsh.erp.constants.ExceptionConstants;
import com.jsh.erp.datasource.entities.*;
import com.jsh.erp.datasource.vo.PromotionGiftWithMaterial;
import com.jsh.erp.datasource.vo.PromotionItemWithMaterial;
import com.jsh.erp.datasource.vo.PromotionVoDetail;
import com.jsh.erp.service.depotItem.DepotItemService;
import com.jsh.erp.service.material.MaterialService;
import com.jsh.erp.service.promotionHead.PromotionHeadService;
import com.jsh.erp.service.redis.RedisService;
import com.jsh.erp.service.supplier.SupplierService;
import com.jsh.erp.service.systemConfig.SystemConfigService;
import com.jsh.erp.service.unit.UnitService;
import com.jsh.erp.utils.BaseResponseInfo;
import com.jsh.erp.utils.ErpInfo;
import com.jsh.erp.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.jsh.erp.utils.ResponseJsonUtil.returnJson;

@RestController
@RequestMapping(value = "/promotionHead")
@Api(tags = {"促销"})
public class PromotionHeadController {

    @Resource
    private PromotionHeadService promotionHeadService;
    @Resource
    private MaterialService materialService;
    @Resource
    private RedisService redisService;
    @Resource
    private DepotItemService depotItemService;

    /**
     * 新增单据主表及单据子表信息
     *
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping(value = "/addPromotionHeadAndDetail")
    @ApiOperation(value = "新增单据主表及单据子表信息")
    public Object addDepotHeadAndDetail(@RequestBody JSONObject obj, HttpServletRequest request) throws Exception {
        String lanCode = redisService.getLanCodeFromSession(request);
        JSONObject result = ExceptionConstants.standardSuccess(lanCode);
        promotionHeadService.addPromotionHeadAndDetail(obj, request);
        return result;
    }

    /**
     * 新增修改主表及单据子表信息
     *
     * @param obj
     * @param request
     * @return
     * @throws Exception
     */
    @PutMapping(value = "/updatePromotionHeadAndDetail")
    @ApiOperation(value = "新增单据主表及单据子表信息")
    public Object updatePromotionHeadAndDetail(@RequestBody JSONObject obj, HttpServletRequest request) throws Exception {
        String lanCode = redisService.getLanCodeFromSession(request);
        JSONObject result = ExceptionConstants.standardSuccess(lanCode);
        promotionHeadService.updatePromotionHeadAndDetail(obj, request);
        return result;
    }

    @GetMapping(value = "/getDetailList")
    @ApiOperation(value = "明细列表")
    public BaseResponseInfo getDetailList(@RequestParam("headerId") String headerId, HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            JSONObject outer = new JSONObject();
            List<PromotionItemWithMaterial> itemList = new ArrayList<>();
            if (!StringUtil.isEmpty(headerId)) {
                itemList = promotionHeadService.getMaterialById(headerId);
            }
            PromotionHead promotionHead = promotionHeadService.getPromotionHead(headerId);
//            if (promotionHead.getCustom11()==3){
//                if (null != promotionHead.getHoldSupplier() && promotionHead.getHoldSupplier()==2){
//                    List<PromotionSupplier> supplierList = promotionHeadService.getSupplierId(headerId);
//                    outer.put("supplierList",supplierList);
//                }
//            }
//            if (promotionHead.getHoldDepot()==2){
//                List<PromotionDepot> depotList = promotionHeadService.getDepotId(headerId);
//                outer.put("depotList",depotList);
//            }
            if (promotionHead.getPromotionType() == 3) {
                for (PromotionItemWithMaterial item : itemList) {
                    List<PromotionGiftWithMaterial> giftList = promotionHeadService.getMaterialByGiftId(item.getId());
                    if (giftList.size() > 0) {
                        for (PromotionGiftWithMaterial gift : giftList) {
                            gift.setMaterialId(item.getMaterialId());
                        }
                        item.setPromotionGiftList(giftList);
                    }
                }
            }
            outer.put("total", itemList.size());
            outer.put("rows", itemList);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    @GetMapping(value = "/getGiftDetailList")
    @ApiOperation(value = "赠品明细列表")
    public BaseResponseInfo getGiftDetailList(@RequestParam("itemId") String itemId, HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        try {
            JSONObject outer = new JSONObject();
            List<PromotionGiftWithMaterial> itemList = new ArrayList<>();
            if (!StringUtil.isEmpty(itemId)) {
                itemList = promotionHeadService.getMaterialByGiftId(itemId);
            }

            outer.put("total", itemList.size());
            outer.put("rows", itemList);
            res.code = 200;
            res.data = outer;
        } catch (Exception e) {
            e.printStackTrace();
            res.code = 500;
            res.data = "zh".equals(lanCode) ? ErpInfo.FAILED.name : ErpInfo.FAILED.lanCodeName;
        }
        return res;
    }

    /**
     * @Author: wy
     * @Description: 根据数量和金额获取折扣价和折扣金额
     * @DateTime: 2022/8/17 20:59
     * @Params:
     * @Return
     */
//    @GetMapping(value = "/getDiscount")
//    @ApiOperation(value = "根据数量和金额获取折扣价和折扣金额")
//    public BaseResponseInfo getDiscount(@RequestParam("operNumber") BigDecimal operNumber,
//                                        @RequestParam(value = "materialId") String materialId,
//                                        @RequestParam(value = "meId") String meId,
//                                        @RequestParam(value = "prefixNo") String prefixNo,
//                                        @RequestParam(value = "organId") String organId,
//                                        HttpServletRequest request) throws Exception {
//        BaseResponseInfo res = new BaseResponseInfo();
//        String lanCode = redisService.getLanCodeFromSession(request);
//        Map<String, Object> objectMap = new HashMap<>();
//        List<MaterialVo4Unit> mvoList = materialService.getMaterialByMeIdList(meId);
//        MaterialVo4Unit mvo =new MaterialVo4Unit();
//        if (mvoList!=null && mvoList.size()>0){
//            mvo=mvoList.get(0);
//        }
//        if ("CGDD".equals(prefixNo) || "CGRK".equals(prefixNo) || "CGTH".equals(prefixNo)
//                || "QTRK".equals(prefixNo) || "DBCK".equals(prefixNo) || "ZZD".equals(prefixNo) || "CXD".equals(prefixNo)
//                || "PDLR".equals(prefixNo) || "PDFP".equals(prefixNo)|| "XDCG".equals(prefixNo)) {
//            mvo.setBillPrice(mvo.getPurchaseDecimal());
//        } else if ("XSDD".equals(prefixNo) || "XSCK".equals(prefixNo) || "XSTH".equals(prefixNo) || "QTCK".equals(prefixNo) ||"PSD".equals(prefixNo) ||"CJZF".equals(prefixNo) ){
//            //销售价
//            //促销活动
//            PromotionVoDetail head = promotionHeadService.checkMaterialWithCodeExist(mvo.getId(), mvo.getMeId(), mvo.getCommodityUnit(),1);
//            if (head != null) {
//                mvo.setBillPrice(head.getSpecialPrice());
//                mvo.setPriceNote("促销价");
//            }else {
//                if (StringUtils.isNotEmpty(organId)){
//                    Supplier supplier = supplierService.getSupplier(organId);
//                    if (null!=supplier){
//                        if (StringUtils.isEmpty(supplier.getPricePriority())){
//                            mvo.setBillPrice(mvo.getWholesaleDecimal());
//                        }
//                    }else {
//                        mvo.setBillPrice(mvo.getWholesaleDecimal());
//                    }
//                }else {
//                    mvo.setBillPrice(mvo.getWholesaleDecimal());
//                }
//            }
//        }else if ("CXTJ".equals(prefixNo)){
//            mvo.setBillPrice(mvo.getWholesaleDecimal());
//        }
//        BigDecimal fullAmout= mvo.getBillPrice().multiply(operNumber);
//        BigDecimal fullAmounts = BigDecimal.ZERO;
//        BigDecimal creditAmount = BigDecimal.ZERO;
//
//        PromotionVoDetail head = promotionHeadService.getPromotionHeadById(materialId);
//        if (null != head && fullAmout.compareTo(head.getFullAmout()) >= 0) {
//            fullAmounts = divideDiscountPrice(fullAmout, head, creditAmount, fullAmout, objectMap, lanCode);
//            BigDecimal discountPrice = fullAmounts.divide(operNumber, 4, BigDecimal.ROUND_HALF_UP);
//            objectMap.put("priceNote", "zh".equals(lanCode) ? "买满金额减金额" : "Buy the full amount minus the amount");
//            objectMap.put("billPrice", discountPrice);
////            objectMap.put("status", false);
//        } else {
//            objectMap.put("billPrice", mvo.getBillPrice());
//            objectMap.put("priceNote", mvo.getPriceNote());
////            objectMap.put("status", true);
//        }
//        res.code = 200;
//        res.data = objectMap;
//        return res;
//    }
//
//    private void getBillPriceByMvo(MaterialVo4Unit mvo,String grade,String supplierId){
//        String lanCode = systemConfigService.getLanCode();
//        switch (grade){
//            case "1":
//                mvo.setBillPrice(mvo.getWholesaleDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "一级批发价" :"Primary wholesale price");
//                break;
//            case "2":
//                mvo.setBillPrice(mvo.getTwoWholesaleDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "二级批发价" :"Secondary wholesale price");
//                break;
//            case "3":
//                mvo.setBillPrice(mvo.getThreeWholesaleDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "三级批发价":"Tertiary wholesale price");
//                break;
//            case "4":
//                mvo.setBillPrice(mvo.getFourWholesaleDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "四级批发价":"Level 4 Wholesale Price");
//                break;
//            case "5":
//                mvo.setBillPrice(mvo.getFiveWholesaleDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "五级批发价":"Level 5 wholesale price");
//                break;
//            case "6":
//                mvo.setBillPrice(mvo.getCommodityDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "零售价":"retail price");
//                break;
//            case "7":
//                mvo.setBillPrice(mvo.getLowDecimal());
//                mvo.setPriceNote("zh".equals(lanCode) ? "最低销售价":"Minimum selling price");
//                break;
//            case "8":
//                BigDecimal lastSellingPrice = depotItemService.getItemByBarcode(mvo.getMeId(), supplierId, mvo.getId());
//
//                mvo.setBillPrice(lastSellingPrice);
//                mvo.setPriceNote("zh".equals(lanCode) ? "最近采购价":"Latest purchase price");
//                break;
//        }
//    }
//    private BigDecimal divideDiscountPrice(BigDecimal allPrice, PromotionVoDetail head, BigDecimal creditAmount, BigDecimal fullAmout, Map<String, Object> objectMap, String lanCode) {
//        int num = allPrice.divide(head.getFullAmout(), 0, BigDecimal.ROUND_DOWN).intValue();
//        if (num > 0) {
//            for (int i = 0; i < num; i++) {
//                creditAmount = creditAmount.add(head.getCreditAmount());
//            }
//        }
//        fullAmout = fullAmout.subtract(creditAmount);
//        return fullAmout;
//    }


    @GetMapping(value = "/getDiscount")
    @ApiOperation(value = "根据数量和金额获取折扣价和折扣金额")
    public BaseResponseInfo getDiscount(
                                        @RequestParam(value = "allPrice") BigDecimal allPrice,
                                        @RequestParam(value = "materialId") String materialId,
                                        @RequestParam(value = "custom11") Integer custom11,
                                        @RequestParam(value = "organId") String organId,
                                        HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> objectMap = new HashMap<>();
        BigDecimal fullAmout= allPrice;
        BigDecimal fullAmounts= BigDecimal.ZERO;
        BigDecimal creditAmount = BigDecimal.ZERO;

        PromotionVoDetail head=null;
        if (StringUtil.isNotEmpty(organId)) {
            head = promotionHeadService.getPromotionHeadById(materialId,custom11,null,organId);
        }else {
            head = promotionHeadService.getPromotionHeadById(materialId,custom11,1,null);
        }
        if (null != head && allPrice.compareTo(head.getFullAmout()) >= 0) {
            allPrice = divideDiscountPrice(allPrice, head, creditAmount, fullAmout, objectMap, lanCode);
        }else {
            objectMap.put("priceNote","");
        }
//        BigDecimal discountPrice = fullAmounts.divide(operNumber, 4, BigDecimal.ROUND_HALF_UP);
//        objectMap.put("creditAmount",creditAmount);
        objectMap.put("allPrice",allPrice);
        res.code = 200;
        res.data = objectMap;
        return res;
    }
//    @GetMapping(value = "/getDiscount")
//    @ApiOperation(value = "根据数量和金额获取折扣价和折扣金额")
//    public BaseResponseInfo getDiscount(
//                                        @RequestParam(value = "allPrice") BigDecimal allPrice,
//                                        @RequestParam(value = "materialId") String materialId,
//                                        HttpServletRequest request) throws Exception {
//        BaseResponseInfo res = new BaseResponseInfo();
//        String lanCode = redisService.getLanCodeFromSession(request);
//        Map<String, Object> objectMap = new HashMap<>();
//        BigDecimal fullAmout= allPrice;
//        BigDecimal fullAmounts= BigDecimal.ZERO;
//        BigDecimal creditAmount = BigDecimal.ZERO;
//        PromotionVoDetail head = promotionHeadService.getPromotionHeadById(materialId,null);
//        if (null != head && allPrice.compareTo(head.getFullAmout())>=0) {
//            allPrice = divideDiscountPrice(allPrice, head, creditAmount, fullAmout, objectMap,lanCode);
//        }else {
//            objectMap.put("priceNote","");
//        }
////        BigDecimal discountPrice = fullAmounts.divide(operNumber, 4, BigDecimal.ROUND_HALF_UP);
////        objectMap.put("creditAmount",creditAmount);
//        objectMap.put("allPrice",allPrice);
//        res.code = 200;
//        res.data = objectMap;
//        return res;
//    }

    private BigDecimal divideDiscountPrice(BigDecimal allPrice,PromotionVoDetail head,BigDecimal creditAmount,BigDecimal fullAmout,Map<String, Object> objectMap,String lanCode){
        int num = allPrice.divide(head.getFullAmout(), 0, BigDecimal.ROUND_DOWN).intValue();
        if (num > 0){
            for (int i = 0; i <num ; i++) {
                creditAmount=creditAmount.add(head.getCreditAmount());
            }
        }
        fullAmout=fullAmout.subtract(creditAmount);
        objectMap.put("priceNote","zh".equals(lanCode) ? "买满金额减金额" : "Buy the full amount minus the amount");
        return fullAmout;
    }
    /**
     * @Author: wy
     * @Description: 根据数量和金额获取折扣价和折扣金额
     * @DateTime: 2022/8/17 20:59
     * @Params:
     * @Return
     */
    @GetMapping(value = "/getNumber")
    @ApiOperation(value = "根据数量获取满赠活动")
    public BaseResponseInfo getNumber(@RequestParam("operNumber") BigDecimal operNumber,
                                      @RequestParam("materialUnit") String materialUnit,
                                      @RequestParam("materialId") String materialId,
                                      @RequestParam(value = "custom11") Integer custom11,
                                      @RequestParam(value = "priceNote") String priceNote,
                                      @RequestParam(value = "organId") String organId,
                                      HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        if (!"赠品".equals(priceNote) && !"gift".equals(priceNote)){
            String lanCode = redisService.getLanCodeFromSession(request);
            List<MaterialVo4Unit> materialVo4UnitList = new ArrayList<>();

            PromotionVoDetail head=null;
            if (StringUtil.isNotEmpty(organId)) {
                head = promotionHeadService.getPromotionGiveById(materialId,materialUnit,custom11,null,organId);
            }else {
                head = promotionHeadService.getPromotionGiveById(materialId,materialUnit,custom11,1,null);
            }
            if (null != head && operNumber.compareTo(head.getPurchaseQuantity()) >= 0) {
                divideOperNumber(operNumber, head, materialVo4UnitList, lanCode,materialId);
            }
            res.code = 200;
            res.data = materialVo4UnitList;
        }
        return res;
    }
//    @GetMapping(value = "/getNumber")
//    @ApiOperation(value = "根据数量获取满赠活动")
//    public BaseResponseInfo getNumber(@RequestParam("operNumber") BigDecimal operNumber,
//                                      @RequestParam("materialUnit") String materialUnit,
//                                      @RequestParam("materialId") String materialId,
//                                      @RequestParam(value = "priceNote") String priceNote,
//                                      HttpServletRequest request) throws Exception {
//        BaseResponseInfo res = new BaseResponseInfo();
//        if (!"赠品".equals(priceNote) && !"gift".equals(priceNote)){
//            String lanCode = redisService.getLanCodeFromSession(request);
//            List<MaterialVo4Unit> materialVo4UnitList = new ArrayList<>();
//            PromotionVoDetail head = promotionHeadService.getPromotionGiveById(materialId, materialUnit,null);
//            if (null != head && operNumber.compareTo(head.getPurchaseQuantity()) >= 0) {
//                divideOperNumber(operNumber, head, materialVo4UnitList, lanCode,materialId);
//            }
//            res.code = 200;
//            res.data = materialVo4UnitList;
//        }
//        return res;
//    }

    /**
     * @Author: wy
     * @Description: 根据数量和金额获取折扣价和折扣金额
     * @DateTime: 2022/8/17 20:59
     * @Params:
     * @Return
     */
    @GetMapping(value = "/lsGetNumber")
    @ApiOperation(value = "零售根据数量获取满赠活动")
    public BaseResponseInfo lsGetNumber(@RequestParam("operNumber") BigDecimal operNumber,
                                      @RequestParam("materialId") String materialId,
                                      @RequestParam("materialExtendId") String materialExtendId,
                                      @RequestParam(value = "priceNote") String priceNote,
                                      HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        if (!"0".equals(priceNote)){
            String lanCode = redisService.getLanCodeFromSession(request);
            List<MaterialVo4Unit> materialVo4UnitList = new ArrayList<>();
            PromotionVoDetail head = promotionHeadService.lsGetPromotionGiveById(materialExtendId);
            if (null != head && operNumber.compareTo(head.getPurchaseQuantity()) >= 0) {
                divideOperNumber(operNumber, head, materialVo4UnitList, lanCode,materialId);
            }
            res.code = 200;
            res.data = materialVo4UnitList;
        }
        return res;
    }
    @GetMapping(value = "/lsGetPromotionPrice")
    @ApiOperation(value = "零售获取促销特价和满减")
    public BaseResponseInfo lsGetPromotionPrice(
                                      @RequestParam("materialId") String materialId,
                                      @RequestParam("materialExtendId") String materialExtendId,
                                      @RequestParam("promotionType") Integer promotionType,
                                      HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        PromotionVoDetail head = promotionHeadService.lsGetPromotionSpecialById(materialExtendId,materialId,promotionType);
        res.code = 200;
        res.data = head;
        return res;
    }

    public void divideOperNumber(BigDecimal operNumber, PromotionVoDetail head, List<MaterialVo4Unit> materialVo4UnitList, String lanCode,String materialId ) throws Exception {
        BigDecimal divide = operNumber.divide(head.getPurchaseQuantity(), 0, BigDecimal.ROUND_DOWN);
        List<PromotionGift> list = promotionHeadService.getGiftId(head.getPid());
        if (null != list) {
            for (PromotionGift promotionGift : list) {
                MaterialVo4Unit mvo = materialService.getMByMeId(promotionGift.getGiftMeId());
                mvo.setOperNumber(divide.multiply(promotionGift.getGiftQuantity()));
                mvo.setBillPrice(BigDecimal.ZERO);
                mvo.setPriceNote("zh".equals(lanCode) ? "赠品" : "gift");
                mvo.setSourceId(materialId);
                mvo.setStock( materialService.getCurrentStockMapByMaterialId(mvo.getId(),null));
//                StringBuffer str = new StringBuffer();
//                Unit unit = unitService.getUnit(mvo.getUnitId());
//                if (StringUtils.isNotEmpty(unit.getOtherUnit()) && unit.getRatio() != 0) {
//                    str.append("(" + operNumber.divide(BigDecimal.valueOf(unit.getRatio()), 2, BigDecimal.ROUND_HALF_UP) + unit.getOtherUnit() + ")");
//                }
//                if (StringUtils.isNotEmpty(unit.getOtherUnitTwo()) && unit.getRatioTwo() != 0) {
//                    str.append("/(" + operNumber.divide(BigDecimal.valueOf(unit.getRatioTwo()), 2, BigDecimal.ROUND_HALF_UP) + unit.getOtherUnitTwo() + ")");
//                }
//                mvo.setAssistStock(str.toString());
                materialVo4UnitList.add(mvo);
            }
        }
    }


    @PostMapping(value = "/batchSetPromotionTenant")
    @ApiOperation(value = "批量设置商品下发的租户")
    public String batchSetPromotionTenant(@RequestBody JSONObject jsonObject,
                                         HttpServletRequest request)throws Exception {
        String ids = jsonObject.getString("ids");
        String syncTenantId = jsonObject.getString("syncTenantId");
        Map<String, Object> objectMap = new HashMap<>();
        String lanCode = redisService.getLanCodeFromSession(request);
        int res = promotionHeadService.batchSetPromotionTenant(ids,syncTenantId);
        if(res > 0) {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.OK.name : ErpInfo.OK.lanCodeName, ErpInfo.OK.code);
        } else {
            return returnJson(objectMap, "zh".equals(lanCode) ? ErpInfo.ERROR.name : ErpInfo.ERROR.lanCodeName, ErpInfo.ERROR.code);
        }
    }

    @GetMapping(value = "/getSpecial")
    @ApiOperation(value = "根据数量和金额获取折扣价和折扣金额")
    public BaseResponseInfo getSpecial(
            @RequestParam(value = "operNumber") BigDecimal operNumber,
            @RequestParam(value = "materialId") String materialId,
            @RequestParam(value = "custom11") Integer custom11,
            @RequestParam(value = "organId") String organId,
            HttpServletRequest request) throws Exception {
        BaseResponseInfo res = new BaseResponseInfo();
        String lanCode = redisService.getLanCodeFromSession(request);
        Map<String, Object> objectMap = new HashMap<>();

        PromotionVoDetail head=null;
        if (StringUtil.isNotEmpty(organId)) {
            head = promotionHeadService.getSpecial(materialId,custom11,operNumber,null,organId);
        }else {
            head = promotionHeadService.getSpecial(materialId,custom11,operNumber,1,null);
        }
        if (head != null) {
            objectMap.put("flag",true);
            objectMap.put("specialPrice",head.getSpecialPrice());
            objectMap.put("priceNote","zh".equals(lanCode) ? "促销价":"PEK-YVR");
        }else {
            objectMap.put("flag",false);
        }
        res.code = 200;
        res.data = objectMap;
        return res;
    }
}
